package database

import (
	redis2 "JoRedis/interface/redis"
	"JoRedis/interface/reply"
	"JoRedis/protocol"
)

type DB struct {
	index   int
	dictPtr *SyncDict
}

func MakeDB(index int) *DB {
	return &DB{
		index:   index,
		dictPtr: MakeSyncDict(),
	}
}

// ExecCmd 从指令集中取出对应的指令来执行客户端发来的指令
func (this *DB) ExecCmd(redisConn redis2.RedisConnItf, argBytesSlc [][]byte) reply.ReplyItf {
	cmdName := string(argBytesSlc[0])
	// 从指令集中获取指令执行器
	cmdExecutor, ok := cmdKeyExecutorMap[cmdName]
	if !ok {
		return protocol.MakeErrReply("ERR unknown command '" + cmdName + "'")
	}
	// 指令执行函数
	cmdFun := cmdExecutor.cmdExecuteFun
	return cmdFun(this, argBytesSlc[1:])
}
